package com.example.camerademoexcercies1.useless;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.camerademoexcercies1.R;
import com.example.camerademoexcercies1.tool.LogTool;

import java.io.IOException;

public class FlashActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private long firstTime = 0;
    private static FlashActivity mFlash;
    private static Camera camera;
    private static Camera.Parameters parameters;
    private static String previousFlashMode = null;
    private static boolean isOpen = false;
    private Switch mSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash);
        initEvent();
    }

    //    初始化事件，控件
    private void initEvent() {
        mSwitch = findViewById(R.id.flash_switch);
        mSwitch.setOnCheckedChangeListener(this);
    }

    //开关闪光灯
    public void onAndOff(Context context) {
        if (isOpen) {
            say("准备关闭相机");
            closeFlash();
        } else {
            say("准备开启相机");
            openFlash();
        }
    }

    //    开启相机，手电筒其实是摄像头的闪光灯一直开启，parameters.setFlashMode(FLASH_MODE_TORCH)
    private void initCamera() {
        try {
            camera = Camera.open();
            camera.setPreviewTexture(new SurfaceTexture(0));
            say("开启摄像头，添加texture");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (camera != null) {
            parameters = camera.getParameters();
            say("获取参数");
        }
    }

    //    若无camera则初始化，然后开启闪光灯
    private void openFlash() {
        if (camera == null) {
            initCamera();
            say("初始化完成");
        }
        if (camera != null) {
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(parameters);
            say("设置FLASH_MODE_TORCH参数");
            camera.startPreview();
            isOpen = true;
            say("开启预览，isOpen = "+isOpen);
        }
    }

    //    停止闪光灯并关闭camera
    public void closeFlash() {
        if (camera != null) {
            camera.stopPreview();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(parameters);
            camera.release();
            camera = null;
            isOpen = false;
            say("关闭闪光灯并清空Camera,isOpen = "+isOpen);
        }
    }


    //    开关动画
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.flash_switch) {
            if (buttonView.isChecked()) {
                mSwitch.setText("开启");
            } else {
                mSwitch.setText("关闭");
            }
            onAndOff(this);
        }
    }

    //    返回退出功能
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) {
                Toast.makeText(FlashActivity.this, "再按一次退出程序", (int) 2000).show();
                firstTime = secondTime;
                return true;
            } else {
                finish();
                System.exit(0);
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    //    打印
    public void say(String word) {
        LogTool.log(word);
    }
}